package scenes

import (
	"math"
	"ys.calc/role"
)

type Scenes struct {
	role.Role
	TotalDmg         int
	WeaponCode       string
	SuitName         string
	SubGroupStr      string
	WeaponSpFunc     func(r *role.Role)
	ReactionIncrease float64
	Enemy            Enemy
	DefenceReduce    float64
	ResistanceReduce float64
	Team             string
}
type Enemy struct {
	Level      float64
	Resistance float64
}

func InitDefault90Enemy() Enemy {
	return Enemy{90, 0.1}
}

func (scenes *Scenes) CalcDmg() int {
	r := scenes.Role
	if r.Reaction > 1.0 {
		masteryIncrease := 2.78 * r.Mastery / (r.Mastery + 1400)
		r.Reaction = r.Reaction * (1 + masteryIncrease + scenes.ReactionIncrease)
	}
	enemy := scenes.Enemy

	clv := r.Level + 100
	defectionFactor := clv / (clv + (1-scenes.DefenceReduce)*(enemy.Level+100))

	resistanceFactor := 1.0
	if enemy.Resistance > scenes.ResistanceReduce {
		resistanceFactor = 1 - (enemy.Resistance - scenes.ResistanceReduce)
	} else if enemy.Resistance < scenes.ResistanceReduce {
		resistanceFactor = 1 - (enemy.Resistance-scenes.ResistanceReduce)/2
	}

	critExpectedValue := r.CritDmg * math.Min(r.CritRate, 100) / 10000
	totalDmg := r.SkillDmg * (r.Atk + r.AtkEx) * (1 + r.Increase/100) * (1 + critExpectedValue) * r.Reaction * defectionFactor * resistanceFactor
	scenes.TotalDmg = int(totalDmg)
	return scenes.TotalDmg
}
